package com.union.core.organization.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.organization.dao.OrganizationParamDao;
import com.union.core.organization.model.OrganizationManager;
import com.union.core.organization.model.OrganizationParam;

public class OrganizationParamDaoImpl implements OrganizationParamDao{
	
	private static Logger log = Logger.getLogger(OrganizationParamDaoImpl.class.getName());
	private DBOperation opera;
	private OrganizationParam param;
	
	public OrganizationParamDaoImpl(){
		
	}
	
	public OrganizationParam query(int organization_id){
		OrganizationParam param = null;
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			
			String sql = "select" 
				   + " param_id,organization_id,organization_name,grade,add_name," 
				   + " add_time,updae_name,update_time" 
				   + " from organization_param" 
				   + " where organization_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, organization_id);
			ResultSet res = opera.executeQuery();
			while(null!=res && res.next()){
				param = new OrganizationParam();
				param.setParam_id(res.getInt(1));
				param.setOrganization_id(res.getInt(2));
				param.setOrganization_name(res.getString(3));
				param.setGrade(res.getInt(4));
				param.setAdd_name(res.getString(5));
				param.setAdd_time(res.getString(6));
				param.setUpdae_name(res.getString(7));
				param.setUpdate_time(res.getString(8));
			}
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return param;
	}
	
	public List<OrganizationParam> queryList(int grade){
		List<OrganizationParam> ParamList = new ArrayList<OrganizationParam>();
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "select" 
				   + " param_id,organization_id,organization_name,grade,add_name," 
				   + " add_time,updae_name,update_time" 
				   + " from organization_param" 
				   + " where grade=?"
				   + " order by param_id";
			opera.prepStatment(sql);
			opera.setInt(1, grade);
			ResultSet res = opera.executeQuery();
			int i = 0;
			while (res.next()) {
				param = new OrganizationParam();
				param.setParam_id(res.getInt(1));
				param.setOrganization_id(res.getInt(2));
				param.setOrganization_name(res.getString(3));
				param.setGrade(res.getInt(4));
				param.setAdd_name(res.getString(5));
				param.setAdd_time(res.getString(6));
				param.setUpdae_name(res.getString(7));
				param.setUpdate_time(res.getString(8));
				ParamList.add(i, param);
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return ParamList;
	}
	
	/**
	 * 添加机构参数
	 * @param param
	 * @return
	 */
	public boolean add(OrganizationParam param){
		boolean result = false;
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "insert into organization_param(organization_id,organization_name,grade,add_name,add_time)"
				+ " values (?,?,?,?,SYSDATE())";
			opera.prepStatment(sql);
			opera.setInt(1, param.getOrganization_id());
		    opera.setString(2, param.getOrganization_name());
		    opera.setInt(3, param.getGrade());
		    opera.setString(4, param.getAdd_name());
			
		    opera.execute();
		    result = opera.transcationCommit(); 
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
	
	/**
	 *更改机构参数
	 * @param param
	 * @return
	 */
	public boolean edit(OrganizationParam param){
		boolean result = false;
		
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			
			String sql = "update organization_param set organization_name=?,updae_name=?,update_time=SYSDATE()"
				   + " where organization_id=?";
			
			opera.prepStatment(sql);
			opera.setString(1, param.getOrganization_name());
		//	opera.setInt(2, param.getGrade());
			opera.setString(2, param.getUpdae_name());
			opera.setInt(3, param.getOrganization_id());
			
			opera.executeUpdate();
			result = opera.transcationCommit();
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
	
	/**
	 * 删除机构参数
	 * @param organization_id
	 * @return
	 */
	public boolean delete(int organization_id){
		boolean result = false;
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			
			String sql = "delete from organization_param where organization_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, organization_id);
			opera.execute();
			opera.transcationCommit();
		}catch(Exception e){
			log.error(e);
		}finally{
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
}